package com.ctg.behavior.calc.impala.transfer.funnel;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.common.CtSQLConsts;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.report.funnel.CtCalcFunnelStepReport;
import com.ctg.behavior.calc.impala.transfer.CtFilterTransfer;
import com.ctg.behavior.calc.utils.CtCalcSQLUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcFilter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtFunnelStepUserIdsTransfer extends CtFilterTransfer {

	/******************************************************
	 * @apiNote: calcObject中包含漏斗步骤计算所需要的所有数据：步骤条件、全局条件、分组信息
	 */
	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {

		if (!(calcObject instanceof CtCalcFunnelStepReport)) {
			log.warn("calcObject is not CtCalcFunnelStepReport, class={}", calcObject.getClass());
			return "";
		}
		CtCalcFunnelStepReport funnelStep = (CtCalcFunnelStepReport) calcObject;

		StringBuilder impalaSQL = new StringBuilder();

		String alias = "fsuids" + funnelStep.getStepNo();
		// 1. SELECT 部分：日期(天:YYYY-MM-DD), byFields中字段, COUNT(*) total
		{
			impalaSQL.append(" SELECT DISTINCT ");
			// event_date
			impalaSQL.append(alias).append(".user_id AS userId ");
		}
		// 2. 表关联
		{
			// 根据APP_ID来决定查询哪个表
			impalaSQL.append(" FROM a_event_parquet_").append(funnelStep.getAppId());
			impalaSQL.append(" ").append(alias).append(" WHERE 1=1 ");
		}
		// 3. 查询条件 - 步骤中过滤条件
		{
			CtCalcFilter stepFilter = funnelStep.getFilter();
			String sqlStepFilter = super.doTransfer(stepFilter, CtSQLParam.createAliasParam(alias));
			log.info("sqlStepFilter={}", sqlStepFilter);
			if (!CtCalcSQLUtils.isEmptyCondtions(sqlStepFilter)) {
				impalaSQL.append(" AND ").append(sqlStepFilter);
			}
		}
		// 4. 查询条件 - 全局过滤条件
		{
			CtCalcFilter globalFilter = funnelStep.getGlobalFilter();
			String sqlGlobalFilter = super.doTransfer(globalFilter, CtSQLParam.createAliasParam(alias));
			log.info("sqlGlobalFilter={}", sqlGlobalFilter);
			if (!CtCalcSQLUtils.isEmptyCondtions(sqlGlobalFilter)) {
				impalaSQL.append(" AND ").append(sqlGlobalFilter);
			}
		}
		// 5.事件查询。
		{
			String eventId = funnelStep.getEventId();
			impalaSQL.append(" AND ").append(alias).append(".event='").append(eventId).append("'");
		}
		// 6.时间段
		{
			String fromDate = funnelStep.getFromDate();
			if (StringUtils.isNotBlank(sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_FROM))) {
				fromDate = sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_FROM);
			}
			if (StringUtils.isNotBlank(fromDate)) {
				impalaSQL.append(" AND ").append(alias).append(".event_date >='").append(fromDate).append("'");
			}

			String toDate = funnelStep.getToDate();
			if (StringUtils.isNotBlank(sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_TO))) {
				toDate = sqlParam.getParam(CtSQLConsts.KEY_FILTER_DATE_TO);
			}
			if (StringUtils.isNotBlank(toDate)) {
				impalaSQL.append(" AND ").append(alias).append(".event_date <='").append(toDate).append("'");
			}
		}
//		// 7.指定分组字段的值限制
//		{
//			if (!funnelStep.getSliceByValues().isEmpty() && !CtFieldConsts.FIELD_KEY_ALL.equals(funnelStep.getSliceByValues().get(0))) {
//				List<String> byFields = funnelStep.getByFields();
//				for (int index = 0; index < byFields.size(); index++) {
//					impalaSQL.append(" AND ");
//					CtCalcPropertyType valueType = CtCalcPropertyUtils.getValueType(byFields.get(index));
//					impalaSQL.append(" fsuids.").append(valueType.getProperty());
//					impalaSQL.append(" = '").append(funnelStep.getSliceByValues().get(index)).append("' ");
//				}
//			}
//		}
		// 7. 指定上一步骤的UserIds过滤条件
		{
			String userIdsFilter = sqlParam.getParam(CtSQLConsts.KEY_FILTER_SQL);
			log.info("userIdsFilter={}", userIdsFilter);
			if (!StringUtils.isEmpty(userIdsFilter)) {
				impalaSQL.append(" AND ").append(alias).append(".user_id IN ( ");
				impalaSQL.append(sqlParam.getParam(CtSQLConsts.KEY_FILTER_SQL));
				impalaSQL.append(" ) ");
			}
		}
//		// 8. 分组(GROUP BY) - 若是整体，则是对日期分组统计
//		{
//			impalaSQL.append(" GROUP BY fsuids.user_id ");
//		}

		return impalaSQL.toString();

	}

}
